<html lang="en">

<head>
    <meta charset="utf-8" />
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="/js/my.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <title>发布新闻</title>
</head>

<body>
    <span style="position: absolute;right: 2%;">
        <a href="/login" id="login">登录</a>
        <a href="/modify_pass" onclick="" id="user_name"></a>
        <a id="log_out" style="display: none;" onclick="logout();">退出</a>
    </span>

    <div align="center" style="width: 50%; margin: auto;">
        <input type="text" id="issue_title" style="width: 100%;" name="issue_title" value=""></input><br>
        <hr><br>
        <textarea id="issue_article" name="issue_article" rows="15" style="width: 100%;"></textarea><br><br>
        <button id="issue_bnt" onclick="issue();">提交</button><br>
        <span id="msg" style="color: red;"></span>
    </div>
    <script>
        function issue() {
            var title = document.querySelector("#issue_title").value;
            var article = document.querySelector("#issue_article").value;
            $.ajax({
                type: "POST",
                url: "/news/issue/submit",
                data: { "title": title, "content": article },
                success: function (result) {
                    window.location.replace("/news/" + result.id);  //跳转到新闻详情页
                },
            }).fail(function (result, result1, result2) {
                $('#msg').html(result.responseText);
            });
        };
        function logout() {
            //方案一:前端删除jwt这个cookie, 把expires设为过去的一个时间。这么做的前提是HttpOnly必须为false
            // document.cookie = "jwt=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=localhost;";
            // show_login();
            //方案二:通过后端删除Cookie
            $.ajax({
                url: "/logout",
                method: "get",
                success: function (result) {
                    show_login();
                }
            })
        };
        // 客户端从sessionStorage里获取auth_token，拿auth_token和bid询问服务端当前用户是不是作者本人
        $(document).ready(function () {//页面加载完成后执行
            $.ajax({
                url: "/user",
                method: "get",
                success: function (result) {
                    if (result.Name) {
                        show_out(result.Name);
                    }
                }
            })
        });
    </script>
</body>